/**
 * Created by meiguizhinian on 2018/7/9
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//TODO：等待完成
//对象 let obj = new ll.Obj(param)
(function () {
	jssupport.UnblockMe = function (map, exit=null, size=null) {
		if (map) {
			this.setMap(map, exit, size);
		}
	};
	let p = jssupport.UnblockMe.prototype;
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	p.clear = function () {
		//木块移动的即时位置
		this._mapString = "";
		this._exit = null;//Point<2, int16_t>
		this._map = new jssupport.RectCover();

		this._handbook = "";
		this._handbookSteps = new LLArray();

		this.living = new jssupport.RectCover();
		//木块移动的步骤
		this.step = new LLArray();
	};

	p.setMap = function(map, exit=null, size=null) {
		this.clear();
		if (!exit) {
			exit = cc.p(0,0);
		}
		if (!size) {
			size = cc.size(0 ,0);
		}
		if (!jssupport.UnblockMeSolver.defaultExitOrSize(map, exit, size)) {
			return;
		}
		this._mapString = map;
		this._exit = exit;
		this._map.box = size;

		let character = {};
		jssupport.StringHelper.count_all(character, map, ".");
		let pos;
		for (let it in character) {
			if (!character.hasOwnProperty(it)) {
				continue;
			}
			pos = [];
			jssupport.StringHelper.find_all(pos, map, it);
			let wood  = cc.rect(0,0,0,0);
			wood.name = it;

			let x = pos[0]%this._map.box.width;
			let y = Math.floor(pos[0]/this._map.box.height);
			wood.x = x;
			wood.y = y;
			if (pos[1]-pos[0]===1) {//横向
				wood.width = character[it];
				wood.height = 1;
			} else {
				wood.width = 1;
				wood.height = character[it];
			}
			this._map.children.array.push(wood);
		}
	};

	p.getMapString = function() {return this._mapString;};
	p.getExit = function() {return this._exit;};
	p.getSize = function() {return {width:this._map.box.width, height:this._map.box.height};};
	p.getMap = function() {return this._map;};
	//所有块的位置和大小
	p.getWoods = function() {return this._map.children;};
	p.getWoodByName = function(name) {
		let idx = 0;
		for (let i=0; i!==this._map.children.array.length; ++i) {
			if (this._map.children.array[i].name===name) {
				idx = i;
				break;
			}
		}
		return this._map.children.array[idx];
	};

	p.setHandbook = function(handbook) {this._handbook = handbook;};
	p.getHandbook = function() {
		if (this._handbook.length===0) {

		}
		return this._handbook;
	};
	/**
	 *	获取怎么移动木块
	 */
	p.getHandbookSteps = function() {
		if (this._handbookSteps.empty()) {
			let book = this.getHandbook();
			let length = book.length/5;
			for (let i=0; i!==length; ++i) {
				let step = book.substr(i*5, 5);
				let p = cc.p(0, 0);
				p.name = step[0];
				p.x = Number(step.substr(1, 2));
				p.y = Number(step.substr(3, 2));
				this._handbookSteps.push(p);
			}
		}
		return this._handbookSteps;
	};

	p.play = function(w, h, uw, uh) {
		this.living.clear();
		this.living.box.width = w;
		this.living.box.height = h;
		this.living.unit.width = uw;
		this.living.unit.height = uh;
		this.replay();
	};
	p.replay = function() {
		this.living.children.clear();
		let unit = this.living.unit;
		for (let i=0; i!==this._map.children.array.length; ++i) {
			let f = this._map.children.array[i];
			let rect = {x:f.x*unit.width, y:f.y*unit.height, width:f.width*unit.width, height:f.height*unit.height};
			rect.name = f.name;
			this.living.children.array.push(rect);
		}
	};
})();

